package demo_0714;

public class T13_0330 {
    // 插入排序
    public static void insertSort(int[] array){
        // write code  here
        for (int i = 1; i < array.length; i++) {
            int j, tmp = array[i];
            for (j = i - 1; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] =  array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }


    // 希尔排序
    public static void shellSort(int[] array){
        // write code  here
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array,gap);
        }
    }
    private static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int j = i - gap;
            int tmp = array[i];
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                }else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {1,5,4,2,3,6};
        shellSort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();

        int[] arr2 = {1,5,4,2,3,6};
        insertSort(arr2);
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + " ");
        }
        System.out.println();
    }
}
